<template>
  <!-- 相关方评定内容设置 -->
  <div class="relatedjudgeitem">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >

        <!-- 左上方菜单栏 -->
        <template slot="menuLeft">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            plain
            v-if="permission.relatedjudgeitem_delete"
            @click="handleDelete">
            删 除
          </el-button>
        </template>
      <template slot="fjForm">
        <div>
          <el-upload
            action="/api/fileManager/upload/file"
            :headers="SetHeaders"
            :file-list="fjFileList"
            :before-upload="beforeUploadFJ"
            :on-remove="handleRemoveFJ"
            :on-success="uploadFJSuccess"
          >
            <el-button size="small" type="primary">选择附件</el-button>
          </el-upload>
        </div>
      </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove
} from "@/api/basedata/interestedpartymange/relatedjudgeitem";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      selectionList: [],
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchLabelWidth:100,
        border: true,
        index: false,
        selection: true,
        viewBtn: true,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "安全类别",
            prop: "lb",
            align: "center",
            labelWidth:100,
            search: true,
            hide:true,
            display:false,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=safetyFacilities_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "请输入类别",
              trigger: "blur"
            }]
          },
          {
            label: "大类",
            prop: "p_name",
            align: "center",
            labelWidth:120,
            row: false,
          },
          {
            label: "小类",
            prop: "p_name",
            align: "center",
            labelWidth:120,
            row: false,
          },
          {
            label: "安全设施名称",
            prop: "area",
            align: "center",
            span:24,
            search: true,
            labelWidth:120,
          },
          {
            label: "到期时间查询",
            prop: "ppf",
            align: "center",
            width: 120,
            search: true,
            hide:true,
            display:false,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=due_date",
            props: {
              label: "dictValue",
              value: "dictKey",
            }
          },
          {
            label: "具体到期时间查询",
            prop: "tyu",
            align: "center",
            searchLabelWidth:140,
            row: false,
            type: "date",
            search: true,
            hide: true,
            display:false,
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "设计工艺设施",
            align: "center",
            prop: "hzxmqd",
            labelWidth:120,
            span: 24,
            maxlength: 150
          },
          {
            label: "检测时间",
            prop: "tyu",
            labelWidth:120,
            align: "center",
            row: false,
            type: "date",
            search: true,
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "到期时间",
            labelWidth:120,
            prop: "tyu",
            align: "center",
            row: false,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "数量",
            prop: "area",
            align: "center",
            labelWidth:120,
            span: 12,
            row: false,
          },
          {
            label: "状态",
            prop: "status",
            align: "center",
            search: true,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=safetyFacilities_status",
            props: {
              label: "dictValue",
              value: "dictKey",
            }
          },
          {
            label: "绑定区域",
            labelWidth:120,
            prop: "arear",
            align: "center",
            hide:true,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=safetyFacilities_status",
            props: {
              label: "dictValue",
              value: "dictKey",
            }
          },
          {
            label: "备注",
            labelWidth:120,
            prop: "marK",
            hide: true,
            type: "textarea",
            span: 24,
            maxlength: 100
          },
          {
            label: "附件",
            labelWidth:120,
            hide: true,
            prop: "fj",
            align: "center",
            row: false,
            slot: true,
            formslot: true,
            viewDisplay: false,
          },
        ]
      },
      
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    // 按钮权限
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.relatedjudgeitem_add, false),
        viewBtn: this.vaildData(this.permission.relatedjudgeitem_view, false),
        delBtn: this.vaildData(this.permission.relatedjudgeitem_delete, false),
        editBtn: this.vaildData(this.permission.relatedjudgeitem_edit, false)
      };
    },

    // 勾选项id
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {

    // 新增
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        },
        error => {
          loading();
          window.console.log(error);
        }
      );
    },

    // 修改
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        },
        error => {
          loading();
          console.log(error);
        }
      );
    },

    // 删除一条
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },

    // 删除多条
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },

    // 打开编辑、新增、查看弹窗前
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 勾选
    selectionChange(list) {
      this.selectionList = list;
    },

    // 清空勾选
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    // 更换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },

    // 更换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 加载数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style></style>